package com.zx.JDK特性.jdk1_9.streamD;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

public class StreamDemo {

    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(10, 2, 11, 8, 20, 40, 9);
        // takeWhile 当达到一定条件就结束,也就是遇到第一个false的数据时，结束
        list.stream().takeWhile(x -> x < 15).forEach(x -> System.out.print(x + ", "));
        System.out.println();

        // dropWhile 与 takeWhile相反，返回一个不符合条件的后面的数据
        list.stream().dropWhile(x -> x < 15).forEach(x -> System.out.print(x + ", "));
        System.out.println();

        // 1.8 Stream中的元素不能完全为null，否则空指针异常
        try {
            Stream<Integer> of = Stream.<Integer>of(null);
            of.forEach(System.out::println);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // java9的升级中，允许创建null
        try {
            Stream<Object> of2 = Stream.ofNullable(null);
            of2.forEach(System.out::println);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 注意下面lambda表达式里面一样要用++i。这个i是基础类型形参。
        System.out.println("jdk8循环10次");
        Stream.iterate(1, i -> ++i).limit(10).forEach(x -> System.out.print(x + ", "));

        System.out.println("jdk9循环10次");
        Stream.iterate(1, i -> i <= 10, i -> ++i).forEach(x -> System.out.print(x + ", "));
    }
}
